<?php 

class Ext_Video_Fetch_Download_Adapter_Youku extends Ext_Video_Fetch_Download_Abstract
{
    public function download()
    {
        $result = array(
            'code'      => Ext_Video_Fetch_Download_Result::FAILURE,
            'html'      => '',
            'messages'  => array(),
        );
        
        $client = new Zend_Http_Client();
        $client->setUri($this->_url);
        $client->setConfig(array(
            'maxredirects' => 0,
            'timeout'      => 30));
        $response = $client->request();

        if (!$response->isSuccessful()) {
            $result['messages'][] = $this->_messager->addMessage(self::DOWNLOAD_FAILED);
            return new Ext_Video_Fetch_Download_Result($result['code'], $result['html'], $result['messages']);
        }

        $front = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');
        $options = $bootstrap->getApplication()->getOptions();

        $html = $response->getBody();
        $html = Ext_Utility_String::dIconv("utf-8", $options['charset'], $html);
        
        $result['code'] = Ext_Video_Fetch_Download_Result::SUCCESS;
        $result['html'] = $html;
        return new Ext_Video_Fetch_Download_Result($result['code'], $result['html'], $result['messages']);
    }
}
